package com.hfi.ood.atmsimulator;

import com.google.common.collect.ImmutableMap;
import com.hfi.ood.atmsimulator.transaction.*;
import lombok.Data;

/**
 * @author ChangLiang
 * @date 2020/5/21
 */
@Data
public class TransactionManager {

    private String cardNo;

    private String password;

    final static ImmutableMap<Character, String> map;

    static{
         map = ImmutableMap.<Character, String>builder()
                .put('w', WithdrawTx.class.getSimpleName())
                .put('d', DepositTx.class.getSimpleName())
                .put('q', QueryBalanceTx.class.getSimpleName())
                .put('t', TransferTx.class.getSimpleName()).build();
    }

    public static Transaction createTransaction(Character character) {
        try {
            // 使用反射消除 if-else or switch-case
            Class<?> aClass = Class.forName(Transaction.class.getPackage().getName() + "." + map.get(character));
            return (Transaction) aClass.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return new DoNothingTx();
    }

}
